//
//  StudentList.m
//  Eath
//
//  Created by yuhaya on 14-9-20.
//  Copyright (c) 2014年 yuhaya. All rights reserved.
//

#import "StudentList.h"
#import "Tool.h"
#import "User.h"
#import "Database.h"

@implementation StudentList

- (id) init
{
    self = [super init];  // Call a designated initializer here.
    if (self != nil) {
        
        self.dataList = [[NSMutableArray alloc] init];
        NSDictionary * user = [User loginInfo];
        NSString * user_id = user[@"id"];
        self.gen_id = user_id;
//        DLog(@"==%@",self.gen_id);
        [self network_stus_genid:user_id];
        [[Database sharedDatabase] syncStudents:self.gen_id];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatedata:) name:SYNC_ALL_STUS object:nil];
    }
    return self;
}

- (id) initWithGen:(NSString *) gen_id
{
    self = [super init];  // Call a designated initializer here.
    if (self != nil) {
        
        self.dataList = [[NSMutableArray alloc] init];
        self.gen_id = gen_id;
        [self network_stus_genid:gen_id];
        [[Database sharedDatabase] syncStudents:self.gen_id];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatedata:) name:SYNC_ALL_STUS object:nil];
    }
    return self;
}

- (void) updatedata:(NSNotification*)notification
{
    [self network_stus_genid:self.gen_id];
    
}
+ (id) shareStudentListWithGen:(NSString *) gen_id
{
    static StudentList * shareStudentList = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        shareStudentList = [[self alloc] initWithGen:gen_id];
    });
    return shareStudentList;
}

- (void) network_stus_genid:(NSString *)gen_id
{
    Database * databases = [Database sharedDatabase];
    self.dataList = [databases genStudents:gen_id];
    self.StuList = [self getStudentList];
    [self.detegate stus_load_over];
    DLog(@"%@",self.StuList);
    [[NSNotificationCenter defaultCenter] postNotificationName:STUS_CHANGE object:self userInfo:nil];
}

+ (id) shareStudentList
{
    static StudentList * shareStudentList = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        shareStudentList = [[self alloc] init];
    });
    return shareStudentList;
}

/**
 *  删除学生
 *
 *  @param identify 学生的identify
 *
 *  @return BOOL
 */
- (Boolean) deleteStu:(NSString *)identify;
{
    return YES;
}

- (NSMutableArray *) getStudentList
{
    NSMutableArray * data = [[NSMutableArray alloc] init];
    for (int i = 0; i < [self.dataList count]; i++) {
        Student * stu = [[Student alloc] initWithId:self.dataList[i][@"id"] andName:self.dataList[i][@"name"] andAge:[self.dataList[i][@"age"] integerValue] andSex:[self.dataList[i][@"sex"] integerValue] andRelation:self.dataList[i][@"relation"] andSchool:self.dataList[i][@"school"] andMain:[self.dataList[i][@"main"] boolValue] andGrade:self.dataList[i][@"grade"] andCard:self.dataList[i][@"card"] andIndex:[self.dataList[i][@"index"] integerValue] andClassName:self.dataList[i][@"class_name"] andEnrolTime:self.dataList[i][@"enrol_time"] andSchoolId:self.dataList[i][@"school_id"]];
        stu.index = i;
        data[i] = stu;
    }
    return data;
}

- (Student *) getStudentByIdentify:(NSString *)identify
{
    for (int i = 0; i < [self.dataList count]; i++) {
        
        if([self.dataList[i][@"id"] isEqualToString:identify]){
            Student * stu = [[Student alloc] initWithId:self.dataList[i][@"id"] andName:self.dataList[i][@"name"] andAge:[self.dataList[i][@"age"] integerValue] andSex:[self.dataList[i][@"sex"] integerValue] andRelation:self.dataList[i][@"relation"] andSchool:self.dataList[i][@"school"] andMain:[self.dataList[i][@"main"] boolValue] andGrade:self.dataList[i][@"grade"] andCard:self.dataList[i][@"card"] andIndex:[self.dataList[i][@"index"] integerValue] andClassName:self.dataList[i][@"class_name"] andEnrolTime:self.dataList[i][@"enrol_time"] andSchoolId:self.dataList[i][@"school_id"]];
            return stu;
        }else{
            continue;
        }
    }
    return nil;
}
@end
